package com.tianqu.tms.www.permission.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.UserFunctionDto;
import com.tianqu.tms.entity.UserClass;
import com.tianqu.tms.entity.UserRole;
import com.tianqu.tms.www.permission.actionform.PermissionCreationInputActionForm;
import com.tianqu.tms.www.permission.viewhelper.PermissionManageViewHelper;

/**
 * @author zhangxueliang
 * 权限管理确认action
 */
public class PermissionCreationConfirmAction extends TmsAction{
	private Logger logger = Logger.getLogger(PermissionCreationConfirmAction.class);
	public static final String CFUNCIDSTR = "cFuncIdStr";
	@SuppressWarnings("unchecked")
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		PermissionManageViewHelper viewhelper = new PermissionManageViewHelper();
		PermissionCreationInputActionForm permissionForm = (PermissionCreationInputActionForm)form;
		//判断角色名称是否有重复
		UserRole userRole = UserRole.getInstance();
		int flg = userRole.findRoleByRoleName(permissionForm.getRoleName());
		if(flg == 2){
			setErrorsToRequest(request, TmsConstants.IH002 ,null);
			return mapping.getInputForward();
		}
		viewhelper = (PermissionManageViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		List userFunctionList = viewhelper.getUserFunctionList();
		List functionListO = new ArrayList();
		List functionList = new ArrayList();
		String funcIdStr = "";//父类被选择
		String cFuncIdStr = "";//子类被选择
		int checkAll = 1;//判断是否全选择
		for(int i=0;i<userFunctionList.size();i++){
			UserFunctionDto userFunc = new UserFunctionDto();
			userFunc = (UserFunctionDto)userFunctionList.get(i);
						
			if(userFunc.getFuncGrade() == 2){//2级菜单判断是否被选中
				String radio = userFunc.getFuncId();
				int checked = Integer.parseInt(request.getParameter(radio));
				if(checked == 1){
					String funcId = userFunc.getFuncId().substring(0,1)+"000";
					if(funcIdStr.indexOf(funcId)<0){
						funcIdStr = funcIdStr + funcId;
					}
					cFuncIdStr = cFuncIdStr + radio;
					checkAll = 2;	
				}
			}		
		}
		
		if(checkAll == 1){
			setErrorsToRequest(request, TmsConstants.IH003 ,null);
			return mapping.getInputForward();
		}
		//根据所选择的父类funcId str  和 子类 funcId str  确定前台显示list
		for(int i=0;i<userFunctionList.size();i++){
			UserFunctionDto userFunc = new UserFunctionDto();
			userFunc = (UserFunctionDto)userFunctionList.get(i);
			UserFunctionDto codes = new UserFunctionDto();
			if(userFunc.getFuncGrade() == 1){
				if(funcIdStr.indexOf(userFunc.getFuncId()) > -1){
					codes.setFuncGrade(userFunc.getFuncGrade());
					codes.setFuncId(userFunc.getFuncId());
					codes.setFuncName(userFunc.getFuncName());
					functionListO.add(codes);
				}
			}else if(userFunc.getFuncGrade() == 2){
				if(cFuncIdStr.indexOf(userFunc.getFuncId()) > -1){
					codes.setFuncGrade(userFunc.getFuncGrade());
					codes.setFuncId(userFunc.getFuncId());
					codes.setFuncName(userFunc.getFuncName());
					codes.setChecked(1);
					functionListO.add(codes);
				}
			}		
			
		}
		//遍历前台显示的list，加入换行样式字段
		int k = 1;
		int total = 0;
		for(int i=0;i<functionListO.size();i++){
			UserFunctionDto userFuncS = new UserFunctionDto();
			userFuncS = (UserFunctionDto)functionListO.get(i);
			if(userFuncS.getFuncGrade() == 1){
				k = 1;
				total = 0;
				String funcId = userFuncS.getFuncId().substring(0,1);
			
				for (String tmp = cFuncIdStr; tmp != null&&tmp.length()>=funcId.length();){
				  if(tmp.indexOf(funcId) == 0){
				    total ++;
				  }
				  tmp = tmp.substring(1);
				}
				
			}
			if(userFuncS.getFuncGrade() == 2){	
				if(k == total){
					if(k % 2 != 0){
						userFuncS.setTrFlag(3);
					}else{
						userFuncS.setTrFlag(2);
					}
				}else{
					if(k % 2 == 0){
						userFuncS.setTrFlag(2);
					}else{
						userFuncS.setTrFlag(1);
					}
				}
				
				k++;
			}
			
			functionList.add(userFuncS);
		}
		
		uc.setAttribute(CFUNCIDSTR, cFuncIdStr);//把所选择的子类str 存入uc，用于上一步返回时候显示上次选择的子类
		viewhelper.setUserFunctionList(functionList);
		viewhelper.setRoleName(permissionForm.getRoleName());
		viewhelper.setRoleStatus(permissionForm.getRoleStatus());
		UserClass userClass = UserClass.getInstance();
		viewhelper.setUserClassName(userClass.getUserClassNameByUserClass(permissionForm.getUserClassId()));
		viewhelper.setUserClassId(permissionForm.getUserClassId());
		viewhelper.setFirststr(funcIdStr);
		uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
		request.setAttribute("viewhelp", viewhelper);
		
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
